package net.hvacapp.cmvrfforandroid.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.model.resource.Resource;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.ProjectInfoData;
import net.hvacapp.cmvrfforandroid.ui.resource.ExperienceFragment;
import net.hvacapp.cmvrfforandroid.ui.resource.ExperienceFragment_;
import net.hvacapp.cmvrfforandroid.ui.resource.NoticeListFragment;
import net.hvacapp.cmvrfforandroid.ui.resource.NoticeListFragment_;
import net.hvacapp.cmvrfforandroid.ui.resource.ResourceDetailActivity;
import net.hvacapp.cmvrfforandroid.ui.resource.ResourceDetailActivity_;
import net.hvacapp.cmvrfforandroid.widget.CMActionBar;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_resource)
public class ResourceFragment extends BaseFragment implements TabLayout.OnTabSelectedListener{

    @ViewById(R.id.resource_tablayout)
    TabLayout mResourceTabL;

    ExperienceFragment mFragmentEx;
    NoticeListFragment mFragmentNotice;

    public ResourceFragment() {

    }

    @AfterViews
    public void init(){

        mResourceTabL.addTab(mResourceTabL.newTab().setText("体验"), true);
        mResourceTabL.addTab(mResourceTabL.newTab().setText("公告"), false);

        mResourceTabL.setOnTabSelectedListener(this);


        setTabSelection(0);

    }


    /**
     * 设置tab页
     * @param position 0:概况  1:关键词  2:排名
     */
    private void setTabSelection(int position) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);

        switch (position) {
            case 0:
                if (mFragmentEx == null) {
                    mFragmentEx = ExperienceFragment_.builder().arg("typeValue", ProjectInfoData.PROJECT_PHASE_ALL).build();
                    transaction.add(R.id.content_fl, mFragmentEx);
                } else {
                    transaction.show(mFragmentEx);
                }
                break;
            case 1:
                if (mFragmentNotice == null) {
                    mFragmentNotice = NoticeListFragment_.builder().arg("typeValue", ProjectInfoData.PROJECT_PHASE_INPROGRESS).build();
                    transaction.add(R.id.content_fl, mFragmentNotice);
                } else {
                    transaction.show(mFragmentNotice);
                }
                break;
        }

        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mFragmentEx != null) {
            transaction.hide(mFragmentEx);
        }
        if (mFragmentNotice != null) {
            transaction.hide(mFragmentNotice);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                setTabSelection(0);
                break;
            case 1:
                setTabSelection(1);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
